/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Vacunaciones.java
 *
 * Created on 14-dic-2011, 20:31:24
 */
package formularios;

import clases.ConversorRSaDefaultTableModel;
import clases.ProcesarTxt;
import clases.conexion;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Elio
 */
public class Vacunaciones extends javax.swing.JDialog {
    

    /** Creates new form Vacunaciones */
    public Vacunaciones(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        panelVacunacion.setVisible(false);
        /*
         * SELECT b.identificacion, t.descripcion, v.fecha FROM tipovacunacion t, bovino b, vacunacion v
    WHERE b.identificacion = v.identificacion and v.idtipovacunacion = t.idtipovacunacion;
         */
    }

    
      public void Conectar(){
        try{
            mdbc = new conexion();
            mdbc.init();
            Connection conn= mdbc.getMyConnection();
            stmt = conn.createStatement();
        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Problemas al concetarse a la Base de Datos","Error",JOptionPane.ERROR_MESSAGE);
        }
    }
    
     private void CrearTabla(String sql){
      
          try {
            Conectar();
            ResultSet rs = stmt.executeQuery(sql);

      
            tablaVacunaciones.setModel(modelo);
            tablaVacunaciones.setAutoResizeMode(tablaVacunaciones.AUTO_RESIZE_ALL_COLUMNS);
              
                    
            ConversorRSaDefaultTableModel.completar(rs, modelo);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error en la consulta de indice de preñez" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        
    }
     
     
     private conexion mdbc;
     private Statement stmt;
     private DefaultTableModel modelo = new DefaultTableModel();
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnNuevasVacunaciones = new javax.swing.JButton();
        btnHistoricoVacunaciones = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        panelVacunacion = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaVacunaciones = new javax.swing.JTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cbTipoVacunas = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbFechasVacunas = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Control y Seguimiento de Vacunaciones");

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        btnNuevasVacunaciones.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/agregarvacunacion.png"))); // NOI18N
        btnNuevasVacunaciones.setText("Nuevas Vacunaciones");
        btnNuevasVacunaciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevasVacunacionesActionPerformed(evt);
            }
        });
        jPanel1.add(btnNuevasVacunaciones);

        btnHistoricoVacunaciones.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/historicovacunacion1.png"))); // NOI18N
        btnHistoricoVacunaciones.setText(" Histórico de Vacunaciones");
        btnHistoricoVacunaciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHistoricoVacunacionesActionPerformed(evt);
            }
        });
        jPanel1.add(btnHistoricoVacunaciones);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Seguimiento de Vacunaciones");

        tablaVacunaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaVacunaciones);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Tipo de Vacunación:");

        cbTipoVacunas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todas", "Aftosa ", "Vitaminas ", "Calcio" }));
        cbTipoVacunas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTipoVacunasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbTipoVacunas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(224, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbTipoVacunas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Vacunas por Tipo", jPanel3);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Seleccione el año que desee consultar: ");

        cbFechasVacunas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2010", "2009", "2008", "2007", "2006" }));
        cbFechasVacunas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFechasVacunasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbFechasVacunas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(142, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbFechasVacunas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Vacunas por año", jPanel4);

        javax.swing.GroupLayout panelVacunacionLayout = new javax.swing.GroupLayout(panelVacunacion);
        panelVacunacion.setLayout(panelVacunacionLayout);
        panelVacunacionLayout.setHorizontalGroup(
            panelVacunacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVacunacionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelVacunacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelVacunacionLayout.setVerticalGroup(
            panelVacunacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelVacunacionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
            .addComponent(panelVacunacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelVacunacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-470)/2, (screenSize.height-537)/2, 470, 537);
    }// </editor-fold>//GEN-END:initComponents

private void btnNuevasVacunacionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevasVacunacionesActionPerformed

    new ProcesarTxt().LeerControlVacunacion();
    
}//GEN-LAST:event_btnNuevasVacunacionesActionPerformed

private void btnHistoricoVacunacionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHistoricoVacunacionesActionPerformed
    
    String consulta = "SELECT b.identificacion, t.descripcion, v.fecha FROM tipovacunacion t, bovino b, vacunacion v"
            + "    WHERE b.identificacion = v.identificacion and v.idtipovacunacion = t.idtipovacunacion";
    
    CrearTabla(consulta);
    panelVacunacion.setVisible(true);
        
}//GEN-LAST:event_btnHistoricoVacunacionesActionPerformed

private void cbTipoVacunasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTipoVacunasActionPerformed

      String consulta = "SELECT b.identificacion, t.descripcion, v.fecha FROM tipovacunacion t, bovino b, vacunacion v"
            + "    WHERE b.identificacion = v.identificacion and v.idtipovacunacion = t.idtipovacunacion"
               + " and t.idtipovacunacion = '"+cbTipoVacunas.getSelectedIndex()+"'";
       CrearTabla(consulta);
}//GEN-LAST:event_cbTipoVacunasActionPerformed

private void cbFechasVacunasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFechasVacunasActionPerformed

   String consulta = "SELECT b.identificacion, t.descripcion, v.fecha FROM tipovacunacion t, bovino b, vacunacion v "
            + " WHERE b.identificacion = v.identificacion and v.idtipovacunacion = t.idtipovacunacion"
            + " and YEAR(v.fecha) = '"+cbFechasVacunas.getSelectedItem().toString()+"'";
    CrearTabla(consulta);
    
}//GEN-LAST:event_cbFechasVacunasActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Vacunaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Vacunaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Vacunaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Vacunaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Vacunaciones dialog = new Vacunaciones(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHistoricoVacunaciones;
    private javax.swing.JButton btnNuevasVacunaciones;
    private javax.swing.JComboBox cbFechasVacunas;
    private javax.swing.JComboBox cbTipoVacunas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel panelVacunacion;
    private javax.swing.JTable tablaVacunaciones;
    // End of variables declaration//GEN-END:variables
}
